<?php
/*
 * @Author: lokei
 * @Date: 2022-09-28 20:58:17
 * @LastEditors: lokei
 * @LastEditTime: 2022-09-29 17:57:20
 * @Description: 
 */
namespace App\Http\Controllers\Cms\Form;

use App\Common\Tools\RandomTool;
use App\Common\Tools\ResultTool;
use App\Http\Controllers\Controller;
use App\Models\Cms\Form\FormItemModel;
use Illuminate\Http\Request;

class ItemController extends Controller {
    public function list() {
        $items = FormItemModel::get();
        $res = ResultTool::success();
        $res['data']['items'] = $items;
        $res['data']['total'] = count($items);
        return $res;
    }

    public function create(Request $request) {
        $item = new FormItemModel();
        $item->type = $request->input('type');
        $item->name = $request->input('name');
        $item->identity = RandomTool::GetRandStr(24);
        $item->is_on = eval("return " . $request->input('is_on') . ";") ? '1' : '0';
        $item->save();
        return ResultTool::success();
    }

    public function update(Request $request) {
        $item = FormItemModel::where('id', '=', $request->input('id'))->first();
        $item->type = $request->input('type');
        $item->name = $request->input('name');
        $item->identity = RandomTool::GetRandStr(24);
        $item->is_on = eval("return " . $request->input('is_on') . ";") ? '1' : '0';
        $item->save();
        return ResultTool::success();
    }
}